
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>mmap --- 内存映射文件支持 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="互联网数据处理" href="netdata.html" />
    <link rel="prev" title="signal --- 设置异步事件处理程序" href="signal.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/mmap.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="netdata.html" title="互联网数据处理"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="signal.html" title="signal --- 设置异步事件处理程序"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="ipc.html" accesskey="U">网络和进程间通信</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-mmap">
<span id="mmap-memory-mapped-file-support"></span><h1><a class="reference internal" href="#module-mmap" title="mmap: Interface to memory-mapped files for Unix and Windows."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmap</span></code></a> --- 内存映射文件支持<a class="headerlink" href="#module-mmap" title="永久链接至标题">¶</a></h1>
<hr class="docutils" />
<p>内存映射（mmap）文件对象的行为既像 <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> 又像 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">文件对象</span></a>。 你可以在大部分接受 <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> 的地方使用 mmap 对象；例如，你可以使用 <a class="reference internal" href="re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> 模块来搜索一个内存映射文件。 你也可以通过执行 <code class="docutils literal notranslate"><span class="pre">obj[index]</span> <span class="pre">=</span> <span class="pre">97</span></code> 来修改单个字节，或者通过对切片赋值来修改一个子序列: <code class="docutils literal notranslate"><span class="pre">obj[i1:i2]</span> <span class="pre">=</span> <span class="pre">b'...'</span></code>。 你还可以在文件的当前位置开始读取和写入数据，并使用 <code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code> 前往另一个位置。</p>
<p>内存映射文件是由 <a class="reference internal" href="#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap</span></code></a> 构造函数创建的，其在 Unix和 Windows上是不同的。 无论哪种情况，你都必须为一个打开的文件提供文件描述符以进行更新。 如果你希望映射一个已有的 Python 文件对象，请使用该对象的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code> 方法来获取 <em>fileno</em> 参数的正确值。 否则，你可以使用 <a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a> 函数来打开这个文件，这会直接返回一个文件描述符（结束时仍然需要关闭该文件）。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>如果要为可写的缓冲文件创建内存映射，则应当首先 <a class="reference internal" href="io.html#io.IOBase.flush" title="io.IOBase.flush"><code class="xref py py-func docutils literal notranslate"><span class="pre">flush()</span></code></a> 该文件。 这确保了对缓冲区的本地修改在内存映射中可用。</p>
</div>
<p>对于 Unix 和 Windows版本的构造函数，可以将 <em>access</em> 指定为可选的关键字参数。 <em>access</em> 接受以下四个值之一： <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code>， <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_WRITE</span></code> 或 <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_COPY</span></code> 分别指定只读，直写或写时复制内存，或 <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_DEFAULT</span></code> 推迟到 <em>prot</em> 。 <em>access</em> 可以在 Unix 和 Windows 上使用。如果未指定 <em>access</em>，则 Windows mmap 返回直写映射。 这三种访问类型的初始内存值均取自指定的文件。 分配给 <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code> 内存映射会引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> 异常。分配给 <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_WRITE</span></code> 内存映射会影响内存和基础文件。 分配给 <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_COPY</span></code> 内存映射会影响内存，但不会更新基础文件。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>添加了 <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_DEFAULT</span></code> 常量。</p>
</div>
<p>要映射匿名内存，应将 -1 作为 fileno 和 length 一起传递。</p>
<dl class="class">
<dt id="mmap.mmap">
<em class="property">class </em><code class="sig-prename descclassname">mmap.</code><code class="sig-name descname">mmap</code><span class="sig-paren">(</span><em class="sig-param">fileno</em>, <em class="sig-param">length</em>, <em class="sig-param">tagname=None</em>, <em class="sig-param">access=ACCESS_DEFAULT</em><span class="optional">[</span>, <em class="sig-param">offset</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap" title="永久链接至目标">¶</a></dt>
<dd><p><strong>（ Windows 版本）</strong> 映射被文件句柄 <em>fileno</em> 指定的文件的 <em>length</em> 个字节，并创建一个 mmap 对象。如果 <em>length</em> 大于当前文件大小，则文件将扩展为包含 <em>length</em> 个字节。如果 <em>length</em> 为 <code class="docutils literal notranslate"><span class="pre">0</span></code>，则映射的最大长度为当前文件大小。如果文件为空， Windows 会引发异常（你无法在Windows上创建空映射）。</p>
<p>如果 <em>tagname</em> 被指定且不是 <code class="docutils literal notranslate"><span class="pre">None</span></code> ，则是为映射提供标签名称的字符串。 Windows 允许你对同一文件拥有许多不同的映射。如果指定现有标签的名称，则会打开该标签，否则将创建该名称的新标签。如果省略此参数或设置为 <code class="docutils literal notranslate"><span class="pre">None</span></code> ，则创建的映射不带名称。避免使用 tag 参数将有助于使代码在Unix和Windows之间可移植。</p>
<p><em>offset</em> 可以被指定为非负整数偏移量。 mmap 引用将相对于从文件开头的偏移。 <em>offset</em> 默认为0。 <em>offeset</em> 必须是 <code class="xref py py-const docutils literal notranslate"><span class="pre">ALLOCATIONGRANULARITY</span></code> 的倍数。</p>
</dd></dl>

<dl class="class">
<dt>
<em class="property">class </em><code class="sig-prename descclassname">mmap.</code><code class="sig-name descname">mmap</code><span class="sig-paren">(</span><em class="sig-param">fileno</em>, <em class="sig-param">length</em>, <em class="sig-param">flags=MAP_SHARED</em>, <em class="sig-param">prot=PROT_WRITE|PROT_READ</em>, <em class="sig-param">access=ACCESS_DEFAULT</em><span class="optional">[</span>, <em class="sig-param">offset</em><span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p><strong>(Unix 版本)</strong> 映射文件描述符 <em>fileno</em> 指定的文件的 <em>length</em> 个字节，并返回一个 mmap 对象。如果 <em>length</em> 为 <code class="docutils literal notranslate"><span class="pre">0</span></code> ，则当调用 <a class="reference internal" href="#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap</span></code></a> 时，映射的最大长度将为文件的当前大小。</p>
<p><em>flags</em> specifies the nature of the mapping. <code class="xref py py-const docutils literal notranslate"><span class="pre">MAP_PRIVATE</span></code> creates a
private copy-on-write mapping, so changes to the contents of the mmap
object will be private to this process, and <code class="xref py py-const docutils literal notranslate"><span class="pre">MAP_SHARED</span></code> creates a
mapping that's shared with all other processes mapping the same areas of
the file.  The default value is <code class="xref py py-const docutils literal notranslate"><span class="pre">MAP_SHARED</span></code>.</p>
<p><em>prot</em>, if specified, gives the desired memory protection; the two most
useful values are <code class="xref py py-const docutils literal notranslate"><span class="pre">PROT_READ</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">PROT_WRITE</span></code>, to specify
that the pages may be read or written.  <em>prot</em> defaults to
<code class="xref py py-const docutils literal notranslate"><span class="pre">PROT_READ</span> <span class="pre">|</span> <span class="pre">PROT_WRITE</span></code>.</p>
<p><em>access</em> may be specified in lieu of <em>flags</em> and <em>prot</em> as an optional
keyword parameter.  It is an error to specify both <em>flags</em>, <em>prot</em> and
<em>access</em>.  See the description of <em>access</em> above for information on how to
use this parameter.</p>
<p><em>offset</em> may be specified as a non-negative integer offset. mmap references
will be relative to the offset from the beginning of the file. <em>offset</em>
defaults to 0. <em>offset</em> must be a multiple of <code class="xref py py-const docutils literal notranslate"><span class="pre">ALLOCATIONGRANULARITY</span></code>
which is equal to <code class="xref py py-const docutils literal notranslate"><span class="pre">PAGESIZE</span></code> on Unix systems.</p>
<p>To ensure validity of the created memory mapping the file specified
by the descriptor <em>fileno</em> is internally automatically synchronized
with physical backing store on Mac OS X and OpenVMS.</p>
<p>This example shows a simple way of using <a class="reference internal" href="#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap</span></code></a>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mmap</span>

<span class="c1"># write a simple example file</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;hello.txt&quot;</span><span class="p">,</span> <span class="s2">&quot;wb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Hello Python!</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;hello.txt&quot;</span><span class="p">,</span> <span class="s2">&quot;r+b&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="c1"># memory-map the file, size 0 means whole file</span>
    <span class="n">mm</span> <span class="o">=</span> <span class="n">mmap</span><span class="o">.</span><span class="n">mmap</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="mi">0</span><span class="p">)</span>
    <span class="c1"># read content via standard file methods</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">mm</span><span class="o">.</span><span class="n">readline</span><span class="p">())</span>  <span class="c1"># prints b&quot;Hello Python!\n&quot;</span>
    <span class="c1"># read content via slice notation</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">mm</span><span class="p">[:</span><span class="mi">5</span><span class="p">])</span>  <span class="c1"># prints b&quot;Hello&quot;</span>
    <span class="c1"># update content using slice notation;</span>
    <span class="c1"># note that new content must have same size</span>
    <span class="n">mm</span><span class="p">[</span><span class="mi">6</span><span class="p">:]</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot; world!</span><span class="se">\n</span><span class="s2">&quot;</span>
    <span class="c1"># ... and read again using standard file methods</span>
    <span class="n">mm</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">mm</span><span class="o">.</span><span class="n">readline</span><span class="p">())</span>  <span class="c1"># prints b&quot;Hello  world!\n&quot;</span>
    <span class="c1"># close the map</span>
    <span class="n">mm</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p><a class="reference internal" href="#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap</span></code></a> can also be used as a context manager in a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a>
statement:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mmap</span>

<span class="k">with</span> <span class="n">mmap</span><span class="o">.</span><span class="n">mmap</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">13</span><span class="p">)</span> <span class="k">as</span> <span class="n">mm</span><span class="p">:</span>
    <span class="n">mm</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Hello world!&quot;</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能: </span>Context manager support.</p>
</div>
<p>The next example demonstrates how to create an anonymous map and exchange
data between the parent and child processes:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mmap</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="n">mm</span> <span class="o">=</span> <span class="n">mmap</span><span class="o">.</span><span class="n">mmap</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">13</span><span class="p">)</span>
<span class="n">mm</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Hello world!&quot;</span><span class="p">)</span>

<span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fork</span><span class="p">()</span>

<span class="k">if</span> <span class="n">pid</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>  <span class="c1"># In a child process</span>
    <span class="n">mm</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">mm</span><span class="o">.</span><span class="n">readline</span><span class="p">())</span>

    <span class="n">mm</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Memory-mapped file objects support the following methods:</p>
<dl class="method">
<dt id="mmap.mmap.close">
<code class="sig-name descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.close" title="永久链接至目标">¶</a></dt>
<dd><p>Closes the mmap. Subsequent calls to other methods of the object will
result in a ValueError exception being raised. This will not close
the open file.</p>
</dd></dl>

<dl class="attribute">
<dt id="mmap.mmap.closed">
<code class="sig-name descname">closed</code><a class="headerlink" href="#mmap.mmap.closed" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the file is closed.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.find">
<code class="sig-name descname">find</code><span class="sig-paren">(</span><em class="sig-param">sub</em><span class="optional">[</span>, <em class="sig-param">start</em><span class="optional">[</span>, <em class="sig-param">end</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.find" title="永久链接至目标">¶</a></dt>
<dd><p>Returns the lowest index in the object where the subsequence <em>sub</em> is
found, such that <em>sub</em> is contained in the range [<em>start</em>, <em>end</em>].
Optional arguments <em>start</em> and <em>end</em> are interpreted as in slice notation.
Returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> on failure.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>现在支持可写的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">字节类对象</span></a>。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.flush">
<code class="sig-name descname">flush</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">offset</em><span class="optional">[</span>, <em class="sig-param">size</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.flush" title="永久链接至目标">¶</a></dt>
<dd><p>Flushes changes made to the in-memory copy of a file back to disk. Without
use of this call there is no guarantee that changes are written back before
the object is destroyed.  If <em>offset</em> and <em>size</em> are specified, only
changes to the given range of bytes will be flushed to disk; otherwise, the
whole extent of the mapping is flushed.  <em>offset</em> must be a multiple of the
<code class="xref py py-const docutils literal notranslate"><span class="pre">PAGESIZE</span></code> or <code class="xref py py-const docutils literal notranslate"><span class="pre">ALLOCATIONGRANULARITY</span></code>.</p>
<p><strong>(Windows version)</strong> A nonzero value returned indicates success; zero
indicates failure.</p>
<p><strong>(Unix 版本)</strong> 返回零值以表示成功。 当调用失败时将引发异常。</p>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.move">
<code class="sig-name descname">move</code><span class="sig-paren">(</span><em class="sig-param">dest</em>, <em class="sig-param">src</em>, <em class="sig-param">count</em><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.move" title="永久链接至目标">¶</a></dt>
<dd><p>Copy the <em>count</em> bytes starting at offset <em>src</em> to the destination index
<em>dest</em>.  If the mmap was created with <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code>, then calls to
move will raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.read">
<code class="sig-name descname">read</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">n</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.read" title="永久链接至目标">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> containing up to <em>n</em> bytes starting from the
current file position. If the argument is omitted, <code class="docutils literal notranslate"><span class="pre">None</span></code> or negative,
return all bytes from the current file position to the end of the
mapping. The file position is updated to point after the bytes that were
returned.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>Argument can be omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.read_byte">
<code class="sig-name descname">read_byte</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.read_byte" title="永久链接至目标">¶</a></dt>
<dd><p>Returns a byte at the current file position as an integer, and advances
the file position by 1.</p>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.readline">
<code class="sig-name descname">readline</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.readline" title="永久链接至目标">¶</a></dt>
<dd><p>Returns a single line, starting at the current file position and up to the
next newline.</p>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.resize">
<code class="sig-name descname">resize</code><span class="sig-paren">(</span><em class="sig-param">newsize</em><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.resize" title="永久链接至目标">¶</a></dt>
<dd><p>Resizes the map and the underlying file, if any. If the mmap was created
with <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code> or <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_COPY</span></code>, resizing the map will
raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.rfind">
<code class="sig-name descname">rfind</code><span class="sig-paren">(</span><em class="sig-param">sub</em><span class="optional">[</span>, <em class="sig-param">start</em><span class="optional">[</span>, <em class="sig-param">end</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.rfind" title="永久链接至目标">¶</a></dt>
<dd><p>Returns the highest index in the object where the subsequence <em>sub</em> is
found, such that <em>sub</em> is contained in the range [<em>start</em>, <em>end</em>].
Optional arguments <em>start</em> and <em>end</em> are interpreted as in slice notation.
Returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> on failure.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>现在支持可写的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">字节类对象</span></a>。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.seek">
<code class="sig-name descname">seek</code><span class="sig-paren">(</span><em class="sig-param">pos</em><span class="optional">[</span>, <em class="sig-param">whence</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.seek" title="永久链接至目标">¶</a></dt>
<dd><p>Set the file's current position.  <em>whence</em> argument is optional and
defaults to <code class="docutils literal notranslate"><span class="pre">os.SEEK_SET</span></code> or <code class="docutils literal notranslate"><span class="pre">0</span></code> (absolute file positioning); other
values are <code class="docutils literal notranslate"><span class="pre">os.SEEK_CUR</span></code> or <code class="docutils literal notranslate"><span class="pre">1</span></code> (seek relative to the current
position) and <code class="docutils literal notranslate"><span class="pre">os.SEEK_END</span></code> or <code class="docutils literal notranslate"><span class="pre">2</span></code> (seek relative to the file's end).</p>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.size">
<code class="sig-name descname">size</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.size" title="永久链接至目标">¶</a></dt>
<dd><p>Return the length of the file, which can be larger than the size of the
memory-mapped area.</p>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.tell">
<code class="sig-name descname">tell</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.tell" title="永久链接至目标">¶</a></dt>
<dd><p>Returns the current position of the file pointer.</p>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.write">
<code class="sig-name descname">write</code><span class="sig-paren">(</span><em class="sig-param">bytes</em><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.write" title="永久链接至目标">¶</a></dt>
<dd><p>Write the bytes in <em>bytes</em> into memory at the current position of the
file pointer and return the number of bytes written (never less than
<code class="docutils literal notranslate"><span class="pre">len(bytes)</span></code>, since if the write fails, a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be
raised).  The file position is updated to point after the bytes that
were written.  If the mmap was created with <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code>, then
writing to it will raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>现在支持可写的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">字节类对象</span></a>。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>The number of bytes written is now returned.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mmap.mmap.write_byte">
<code class="sig-name descname">write_byte</code><span class="sig-paren">(</span><em class="sig-param">byte</em><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.write_byte" title="永久链接至目标">¶</a></dt>
<dd><p>Write the integer <em>byte</em> into memory at the current
position of the file pointer; the file position is advanced by <code class="docutils literal notranslate"><span class="pre">1</span></code>. If
the mmap was created with <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code>, then writing to it will
raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p>
</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="signal.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code> --- 设置异步事件处理程序</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="netdata.html"
                        title="下一章">互联网数据处理</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/mmap.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="netdata.html" title="互联网数据处理"
             >下一页</a> |</li>
        <li class="right" >
          <a href="signal.html" title="signal --- 设置异步事件处理程序"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="ipc.html" >网络和进程间通信</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>